'''
启动和项目管理相关的操作
'''
from app import create_app
from flask_script import Manager, Command
from flask_migrate import Migrate,MigrateCommand
from app.models import *
# from gevent import monkey
# monkey.patch_all()
from gevent import pywsgi
from werkzeug.debug import DebuggedApplication
from geventwebsocket.handler import WebSocketHandler

app = create_app()
# 通过Manager()管理项目，并添加数据迁移指令
# 1.创建Manager对象用户托管app
manager = Manager(app)

# 2.创建Migrate对象用于关联要管理的app和db
# migrate = Migrate(app,db)
# 3.再通过Manager对象添加db迁移指令
# manager.add_command('db',MigrateCommand)

# 定义gevent模式实现并发处理
class Serve_forever(Command):
    def __init__(self, app):
        # self.dapp = DebuggedApplication(app, evalex=True)
        self.server = pywsgi.WSGIServer(('0.0.0.0', 5000), app, handler_class=WebSocketHandler)
    def run(self):
        self.server.serve_forever()

manager.add_command('serve_forever', Serve_forever(app))


if __name__ == "__main__":
    manager.run()
   